package leetcode.editor.cn;

/**
题目名字---:旋转图像
题目编号---:48	
解题时间---:2021-09-19 18:03:42
*/
@SuppressWarnings("all")
public class RotateImage{
    public static void main(String[] args) {
        RotateImage rotateImage = new RotateImage();
        int[][] ints = new int[][]{
                {1,2,3},
                {4,5,6},
                {7,8,9}
        };
        rotateImage.rotate1(ints);
        for (int i = 0; i < ints.length; i++) {
            for (int j = 0; j < ints[i].length; j++) {
                System.out.print(ints[i][j]+"  ");
            }
            System.out.println();
        }
    }

    /**
     * n*n矩阵
     * 解法一:线性代数的方法
     * 时间复杂度:O(n*n)
     * @param matrix
     */
    public void rotate1(int[][] matrix) {
        int length = matrix.length;
        //遍历矩阵的一半边--把矩阵转至
        for (int i = 0; i < length; i++) {
            for (int j = i; j < length; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        //翻转矩阵
        for (int i = 0; i < length; i++) {
            for (int j = 0,k=length-1; j <k ; j++,k--) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][k];
                matrix[i][k] = temp;
            }
        }
    }

    /**
     * 解法二:分治法
     * @param matrix
     */
    public void rotate2(int[][] matrix) {

    }
    
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public void rotate(int[][] matrix) {
        int length = matrix.length;
        //遍历矩阵的一半边--把矩阵转至
        for (int i = 0; i < length; i++) {
            for (int j = i; j < length; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        //翻转矩阵
        for (int i = 0; i < length; i++) {
            for (int j = 0,k=length-1; j <k ; j++,k--) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[i][k];
                matrix[i][k] = temp;
            }
        }
    }
}
//leetcode submit region end(Prohibit modification and deletion)


}